package com.hangkong.service.impl;

import com.hangkong.model.BackResult;
import com.hangkong.model.tuser;
import com.hangkong.service.showUserService;

import org.springframework.stereotype.Service;
import com.hangkong.dao.tuserMapper;
import org.springframework.transaction.annotation.Transactional;
import org.springframework.util.DigestUtils;

import javax.annotation.Resource;
import javax.servlet.http.Cookie;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;

import java.util.ArrayList;
import java.util.List;
import java.util.UUID;

/**
 * Created by Administrator on 2017/9/5.
 */
@Service
@Transactional(rollbackFor=Exception.class)
public class showUserServiceImpl implements showUserService {
    @Resource
    private tuserMapper tuserMapper;

   /* @Value("${REDIS_USER_SESSION_KEY}")

    private String REDIS_USER_SESSION_KEY;

    @Value("${SSO_SESSION_EXPIRE}")

    private Integer SSO_SESSION_EXPIRE;
*/
    public List<tuser> getallUser() {
        return tuserMapper.getallUser();
    }

    public Integer deleteUser(String id) {
        return tuserMapper.deleteUser(id,1);
    }

    public List<tuser> user_query(String select_name) {
        return  tuserMapper.selectByname(select_name);
    }

    public void update_user(String id, String username, String password) {
        tuserMapper.update_user(id,username,password);
    }

    public tuser showItem(String id) {
        return tuserMapper.showItem(id);
    }

    public BackResult checkphone(String phone) {

       List<tuser> list= tuserMapper.checkphone(phone);

        if(list.size()==0){
            return BackResult.build(200,"手机号可注册！");
        }else{
            return BackResult.build(400,"手机号已注册！");
        }

    }

    public BackResult saveuser(String name, String email, String phone) {

        tuserMapper.insertuser(name,email,phone);
        return  BackResult.build(200,"注册成功");
    }

    public BackResult loginuser(String name, String pwd, HttpServletRequest request, HttpServletResponse response) {

        List<tuser> list=tuserMapper.loginuser(name,pwd);
        if(list.size()==0){
            return BackResult.build(400,"用户名不存在!");
        }
        tuser user=list.get(0);
        //对比密码
        if(!DigestUtils.md5DigestAsHex(pwd.getBytes()).equals(user.getPwd())){
            return BackResult.build(400, "用户名或密码错误!");
        }

        /*//生成token
        String token= UUID.randomUUID().toString();
        //保存用户之前，把用户对象中的密码清空

        user.setPwd(null);
        //把用户写入redis
        jedisClient.set(REDIS_USER_SESSION_KEY+":"+token, JsonUtils.objectToJson(user));

        //设置session 过期时间

       jedisClient.expire(REDIS_USER_SESSION_KEY+":"+token,SSO_SESSION_EXPIRE);


        //添加写cookie的逻辑，cookie的有效期是关闭浏览器就失效。
        CookieUtils.setCookie(request, response, "TT_TOKEN", token);

*/
//       Cookie c1=new Cookie("name",name);
//        c1.setMaxAge(3600);
//       response.addCookie(c1);
//


        return BackResult.build(200,"用户名密码正确");
    }


}
